package com.example.my_flea_street.controller;

import com.example.my_flea_street.bean.*;
import com.example.my_flea_street.service.impl.AdministratorService;
import com.example.my_flea_street.util.ImageUtil;
import com.example.my_flea_street.util.ResponseHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping(value = "/administrator")
public class AdministratorController {

    private final AdministratorService service;

    @Autowired
    public AdministratorController(AdministratorService service) {
        this.service = service;
    }

    @RequestMapping(value = "/postNotice", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> postNotice(@RequestBody Map<String, String> json ) {
        String time = json.get("time");
        String text = json.get("text");
        service.postNotice(time,text);
        return ResponseHelper.createResponse(0,"成功");
    }

    @RequestMapping(value = "/getInfos", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> getInfos(@RequestBody Map<String, String> json ) {
        int pageNum = Integer.parseInt(json.get("page_no"));
        int pageSize = Integer.parseInt(json.get("page_size"));
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<ReportedUser> pageInfo = new PageInfo<>(service.getInfo());
        ReportedUserPage infoPage = new ReportedUserPage();
        infoPage.setTotal((int)pageInfo.getTotal());
        infoPage.setPages(pageInfo.getPages());
        infoPage.setPageNum(pageInfo.getPageNum());
        infoPage.setData(pageInfo.getList());
        return ResponseHelper.createResponse(0,"成功",infoPage);
    }
    @RequestMapping(value = "/banUser", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> banUser(@RequestBody Map<String, String> json ) {
        int user_id = Integer.parseInt(json.get("user_id"));
        int id = Integer.parseInt(json.get("id"));
        service.banUser(user_id,1);
        service.cancel(id);
        return ResponseHelper.createResponse(0,"成功");
    }
    @RequestMapping(value = "/cancel", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> cancel(@RequestBody Map<String, String> json ) {
        int id = Integer.parseInt(json.get("id"));
        service.cancel(id);
        return ResponseHelper.createResponse(0,"成功");
    }

    @RequestMapping(value = "/getReportedInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> getReportedInfo(@RequestBody Map<String, String> json ) {
        int pageNum = Integer.parseInt(json.get("page_no"));
        int pageSize = Integer.parseInt(json.get("page_size"));
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<ReportedInfo> pageInfo = new PageInfo<>(service.getReportedInfo());
        ReportedInfoPage infoPage = new ReportedInfoPage();
        infoPage.setTotal((int)pageInfo.getTotal());
        infoPage.setPages(pageInfo.getPages());
        infoPage.setPageNum(pageInfo.getPageNum());
        infoPage.setData(pageInfo.getList());
        return ResponseHelper.createResponse(0,"成功",infoPage);
    }
    @RequestMapping(value = "/deleteInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> deleteInfo(@RequestBody Map<String, String> json ) {
        int info_id = Integer.parseInt(json.get("info_id"));
        int id = Integer.parseInt(json.get("id"));
        String picUrl = service.getPic(info_id);
        if (picUrl != null && !picUrl.equals("") && !"null".equals(picUrl))
            ImageUtil.deletePic(picUrl,"info");
        service.deleteInfo(info_id);
        service.cancelInfo(id);
        return ResponseHelper.createResponse(0,"成功");
    }
    @RequestMapping(value = "/cancelInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> cancelInfo(@RequestBody Map<String, String> json ) {
        int id = Integer.parseInt(json.get("id"));
        service.cancelInfo(id);
        return ResponseHelper.createResponse(0,"成功");
    }
    @RequestMapping(value = "/getBandedUser", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> getBandedUser(@RequestBody Map<String, String> json) {
        int pageNum = Integer.parseInt(json.get("page_no"));
        int pageSize = Integer.parseInt(json.get("page_size"));
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<SearchUser> pageInfo = new PageInfo<>(service.getBandedUser());
        SearchUserPage infoPage = new SearchUserPage();
        infoPage.setTotal((int)pageInfo.getTotal());
        infoPage.setPages(pageInfo.getPages());
        infoPage.setPageNum(pageInfo.getPageNum());
        infoPage.setData(pageInfo.getList());
        return ResponseHelper.createResponse(0,"成功",infoPage);
    }

    @RequestMapping(value = "/cancelBanded", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> cancelBanded(@RequestBody Map<String, String> json) {
        int id = Integer.parseInt(json.get("id"));
        service.banUser(id,0);
        return ResponseHelper.createResponse(0,"成功");
    }
}
